import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  // 设置全局API前缀
  app.setGlobalPrefix('api');

  // 启用CORS
  app.enableCors({
    origin: true,
    credentials: true,
  });

  // 静态文件服务 - 提供上传文件的访问
  const uploadsPath = join(process.cwd(), 'uploads');

  app.useStaticAssets(uploadsPath, {
    prefix: '/uploads/',
  });

  // 全局验证管道
  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
      whitelist: true,
      transformOptions: {
        enableImplicitConversion: true, // 启用隐式转换
      },
    }),
  );

  await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
